
jQuery(document).ready(function($) {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: 'multi_uploader'

    });

    $('#fileupload').fileupload('option', {
        dataType : 'json',
        autoUpload : false,
        maxFileSize : 6000000,
        minFileSize : undefined,
        acceptFileTypes: /(\.|\/)(doc|docx|pdf)$/i,
        sequentialUploads: true,
        singleFileUploads: true,
        add : function(e, data) {
            $('#error').removeClass("notifyError");
            $('#progress').show();
            $("#progress .value").replaceAll("开始上传...")
            data.submit();
        },

        change : function(e, data) {

            $('#progress').show();
            $('#progress .value').text("");
            $('#progress .value').css('width', 0 + '%');
        },
        done : function(e, data) {

            $.each(data.result, function(index, file) {

                var vOutput = "";
                var filename=file.name;

                if(file.error ==''){
                if(filename.length>10){
                    filename= filename.substr(0,10)
                }
                vOutput += "&nbsp;<b>" + filename+"... 上传成功！"+ "</b>";
                $("#filename").val(file.key);
                }else{
                vOutput ="&nbsp;<b>" + file.error+ "!</b>"
                }
                if( $("#cv_name").val() == ""){
                    $("#cv_name").val(file.name)
                }
                $("#progress .value").append(vOutput);
                $('#error').removeClass("notifyError");
            });
        },
        progressall : function(e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .value').css('width', progress + '%');
        },
        fail: function (e, data) {
            var error_info= data.files[0].error;
            $('#progress .value').css('width', "auto");
            $("#progress .orangebar").text(error_info);
        }

    });

    jQuery("#cvUpload").validate({
        highlight: function(element, errorClass) {

            $(element).addClass("error");

        },
        unhighlight: function(element, errorClass) {
            $(element).removeClass("error");
            $('#error').removeClass("notifyError");
        },
        rules: {
            cv_name: "required",
            cv_language: "required",
            filename: "required"
        },
        messages: {
            cv_name: "请填写简历名称",
            cv_language: "请选择简历对应的语言",
            filename: "请上传简历文件"
        },
        ignore: [],
        errorClass:"notifyError",
        errorElement:'span',
        showErrors: function(errorMap, errorList) {
            if (this.settings.highlight) {
                for (var i = 0; this.errorList[i]; ++i) {
                    this.settings.highlight.call(this, this.errorList[i].element,
                        this.settings.errorClass, this.settings.validClass);
                }
            }
            if (this.settings.unhighlight) {
                for (var i = 0, elements = this.validElements(); elements[i]; ++i) {
                    this.settings.unhighlight.call(this, elements[i],
                        this.settings.errorClass, this.settings.validClass);
                }
            }

            $('#error').show();
            $("#error").html("");
            if(errorList.length) {
                $("#error").html("<span class='error'>"+errorList[0]['message']+"</span>");

            }
        },

        errorPlacement: function(error, element) {
            error.appendTo($('#error') );
        }
    });


});
